package HoTan.global.DOM;

import java.io.File;
import java.io.IOException;











import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.w3c.dom.*;

public class DOM {
	/**
	 * Khoi tao bien de doc file XML
	 * @param pathFile
	 * @return	1 bien Document chua noi dung cua file XML
	 * @throws Exception
	 */
	public	Document	ReadXMLDOM(String pathFile) throws Exception{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = factory.newDocumentBuilder();
		File file = new File(pathFile);
		Document doc = docBuilder.parse(file);
		return doc;
	}
	
	/**
	 * tim so nut cua XML
	 * @param pathFile
	 * @return so nut cua XML
	 */
	public int NumBerNode	(String pathFile)	{ 
		int numberNode=0;
		try {
			Document doc = ReadXMLDOM(pathFile);
			NodeList list = doc.getElementsByTagName("*");
			numberNode= list.getLength();
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return numberNode;
	}
	
	/**
	 * Them 1 nut con cua ParentNode
	 * @param pathFile
	 * @param ParentNode
	 * @param NodeNeedAdd
	 */
	public	void addNode(String pathFile,String ParentNode, String NodeNeedAdd){
		try {
			Document doc = ReadXMLDOM(pathFile);
			
			Element node = doc.createElement(NodeNeedAdd);
			NodeList list = doc.getElementsByTagName("*");
			for(int i=0;i<list.getLength();i++){
				Element element = (Element) list.item(i);
				String nodeName = element.getNodeName();
				if(nodeName.equals(ParentNode)){
					element.appendChild(node);
				}
				
			}
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * xoa mot nut
	 * @param pathFile
	 * @param Node
	 */
	public	void RemoveNode(String pathFile ,String Node){
		try {
			Document doc = ReadXMLDOM(pathFile);
			
			NodeList list = doc.getElementsByTagName("*");
			for(int i=0;i<list.getLength();i++){
				Element element = (Element) list.item(i);
				String nodeName = element.getNodeName();
				if(nodeName.equals(Node)){
					element.removeChild(element);
				}
				
			}
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
			
	/**
	 * Tim kiem nut cha cua currentNode
	 * @param pathFile
	 * @param currentNode
	 * @return	parentNode
	 */
	public	String SearchParentNode (String pathFile, String currentNode ){
		Element node = null;
		try {
			Document doc = ReadXMLDOM(pathFile);
			
			NodeList list = doc.getElementsByTagName("*");
			for(int i=0;i<list.getLength();i++){
				Element element = (Element) list.item(i);
				String nodeName = element.getNodeName();
				if(nodeName.equals(currentNode)){
					node = (Element) element.getParentNode();
				}
				
			}
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return (node.getNodeName());
	}
	
	/**
	 * Kiem tra file XML co bi validate ko?
	 * @param xmlFile
	 * @return
	 * @throws ParserConfigurationException
	 * @throws IOException
	 */
	public static boolean validateDTD(String xmlFile) throws ParserConfigurationException, IOException{
		try {
		  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		  factory.setValidating(true);
		  factory.setNamespaceAware(true);
		
		  DocumentBuilder builder = factory.newDocumentBuilder();
		
		  builder.setErrorHandler(new MyErrorHandler());
	      builder.parse(new InputSource(xmlFile));
	      return true;
		}catch (ParserConfigurationException pce) {
		      throw pce;
		}catch (IOException io) {
		      throw io;
		}catch (SAXException se){
		      return false;
		}
	}

			
}

